<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>15.闭包的内存泄漏解决方法</title>
</head>

<body>

  <body>
    <div desc="houdunren">在线学习</div>
    <div desc="hdcms">开源产品</div>
  </body>
  <script>
    // 闭包特性中上级作用域会为函数保存数据，从而造成的如下所示的内存泄漏问题
    /*let divs = document.querySelectorAll("div");
    divs.forEach(function (item) {
      item.addEventListener("click", function () {
        console.log(item.getAttribute("desc"));
      });
    });*/
    // 解决方法：
   divs = document.querySelectorAll("div");
    divs.forEach(function (item) {
      let desc = item.getAttribute("desc");
      item.addEventListener("click", function () {
        console.log(desc);
        console.log(item);// item = null 了 无法再打印
      });
      item = null;
    });

  </script>
</body>

</html>